#!/bin/sh
echo
echo
echo "-----------*** Experimental Process auto deplog v1.0 ***-----------"
echo
echo


echo "本脚本用于自动部署"
echo "在脚本所在环境安装 Git & Docker"
echo "在脚本所在路径下进行部署"
echo
echo "autoDeploy.sh"
echo "  --test"
echo "      --.git"
echo "      --server"
echo "          --app"
echo "          --node_modules"
echo "      --tmp"
echo "          --dist"
echo "      --sh"
echo "          --docker-compose"
echo "          --initdb"
echo "              --.sql"
echo
read -p "开始部署?[y/n]:" flag
case $flag in
    [yY]*)
        echo -e "\033[36m开始自动化部署\033[0m"
        ;;
    [nN]*)
        exit
        ;;
    *)
        echo "Just enter y or n, please."
        exit
        ;;
esac


echo
echo -e "\033[36m安装Git...\033[0m"
echo
if type git >/dev/null 2>&1; then
    echo "Git已存在"
else
    if type apt >/dev/null 2>&1; then
        sudo apt-get update
        sudo apt-get install git
    elif type yum >/dev/null 2>&1; then
        sudo yum updata
        sudo yum install git
    elif type dnf >/dev/null 2>&1; then
        sudo dnf check-update
        sudo dnf install git
    elif type pkg >/dev/null 2>&1; then
        sudo pkg update
        sudo pkg install git
    else
        echo "不存在可被识别的包管理器, 请进行相应的配置!"
        exit 1
    fi
fi


echo
echo -e "\033[36m生成SSH Key\033[0m"
echo
if [ -f ~/.ssh/id_rsa.pub ]; then
    ssh -T git@git.oschina.net
    echo "SSH已存在"
else
    ssh-keygen -t rsa -C "CZXyee@163.com"
    echo
    echo "--------请复制以下内容到用户的SSH公钥中--------"
    cat ~/.ssh/id_rsa.pub
    echo "--------请复制以上内容到用户的SSH公钥中--------"
    echo
    echo -n "复制完成/退出?:[y/n]?"
    while read flag
    do

        if (( $flag==y))
        then
            ssh -T git@git.oschina.net
            echo
            echo -e "\033[36mGit配置完成!\033[0m"
            echo
            break
        elif (( $flag==n ))
        then
            echo "退出成功!"
            echo "如有需要请直接进行配置"
            exit 1
        else
            echo "配置失败, 请重试!"
        fi
        echo "复制完成/退出?[y/n]:"
    done
fi


echo
echo -e "\033[36m安装Docker...\033[0m"
echo
if type docker >/dev/null 2>&1; then
    echo "Docker已存在"
else
    if type apt >/dev/null 2>&1; then
        sudo apt-get install docker
    elif type yum >/dev/null 2>&1; then
        sudo yum install docker
    elif type dnf >/dev/null 2>&1; then
        sudo dnf install docker
    elif type pkg >/dev/null 2>&1; then
        sudo pkg install docker
    else
        echo "不存在可被识别的包管理器, 请进行相应的配置!"
        exit 1
    fi
fi


echo
echo -e "\033[36m配置Docker\033[0m"
echo
if type docker >/dev/null 2>&1; then
    echo
    echo -e "Docker配置完成!"
    echo
else
    systemctl start docker.service
    systemctl enable docker.service
    systemctl status docker
    echo "{" >> /etc/docker/daemon.json
    echo "\"registry-mirrors\": [\"https://hub-mirror.c.163.com\",\"https://mirror.baidubce.com\"]" >> /etc/docker/daemon.json
    echo "}" >> /etc/docker/daemon.json
    systemctl restart docker.service
    sudo gpasswd -a root docker
    newgrp docker
    sudo systemctl restart docker
    echo
    echo -e "\033[36mDocker配置完成!\033[0m"
    echo
fi


echo
echo -e "\033[36m配置docker—compose\033[0m"
echo
if type docker-compose >/dev/null 2>&1; then
    echo "docker—compos已存在"
else
    pip install docker-compose -i https://pypi.tuna.tsinghua.edu.cn/simple
    docker-compose version
fi


projecr=test
URL=https://gitee.com/CZXyee/experimental-process
Branch=test
echo
echo -e "\033[36m初始化Server\033[0m"
echo
if [ ! -e "${projecr}/.git/config" ]; then
    git init ${projecr}
    cd ${projecr}
    git config --global credential.helper store
    git remote add origin ${URL}
    git config core.sparsecheckout true
    echo "/*" >> .git/info/sparse-checkout
    echo "!client/*" >> .git/info/sparse-checkout
    echo "!sh/*.sh" >> .git/info/sparse-checkout
    echo "!*.md" >> .git/info/sparse-checkout
    echo "!*.bat" >> .git/info/sparse-checkout
    git fetch origin ${Branch}
    git checkout ${Branch}
    cd ..
else
    echo "初始化已完成!"
fi


echo
echo -e "\033[36m设置文件\033[0m"
echo
cd ${projecr}
mv tmp/dist/* server/public/
rmdir tmp/dist
rmdir tmp
mv sh/* ../
rmdir sh
cd ..


echo
echo -e "\033[36m构建Docker容器...\033[0m"
echo
sudo docker-compose up -d --build


echo
echo -e "\033[36m写入启动器\033[0m"
echo
echo "docker ps" >> stop.sh
echo "sudo docker-compose up -d --build" >> start.sh
echo "docker ps" >> stop.sh


echo
echo -e "\033[36m写入停止器\033[0m"
echo
echo "docker ps" >> stop.sh
echo "docker stop $(docker ps -a -q)" >> stop.sh
echo "docker ps" >> stop.sh

echo
echo -e "\033[36m写入更新器\033[0m"
echo
echo "echo" >> autoSyn.sh
echo "echo 'Synchronization start...'" >> autoSyn.sh
echo "echo" >> autoSyn.sh
echo "cd ${projecr}" >> autoSyn.sh

# echo "git pull origin ${Branch}" >> autoSyn.sh
# echo "git checkout ${Branch}" >> autoSyn.sh
echo "git fetch origin ${Branch}" >> autoSyn.sh
echo "git checkout ${Branch}" >> autoSyn.sh

echo "mv tmp/dist/* server/public/" >> autoSyn.sh
echo "rmdir tmp/dist" >> autoSyn.sh
echo "mv sh/* ../" >> autoSyn.sh
echo "rmdir sh" >> autoSyn.sh
echo "cd .." >> autoSyn.sh
echo "sudo docker-compose up -d --build" >> autoSyn.sh
echo "echo" >> autoSyn.sh
echo "echo 'Synchronization complete!'" >> autoSyn.sh
echo "echo" >> autoSyn.sh


echo ""
echo -e "\033[36mInitialization complete\033[0m"
echo -e "\033[36mOVER!\033[0m"
echo ""

